$(function(){
	$('#tt').datagrid({
		url:'/qyChannelSimulatorListen/getQyChannelSimulatorListenListPage'
	});
	
});

function doSearch(){
	var location=$('#slocationId').val();
	var actionType=$('#sactionTypeId').combobox('getValue');
	var online=$('#isOnlineId').combobox('getValue');
	var version=$('#sversionId').val();
	var osVersion=$('#osVersion').val();
	var heartVersion=$('#sheartVersionId').val();
	var uuid=$('#suuidId').val();
	var clientIp=$('#sclientIpId').val();
	var clientMac=$('#sclientMacId').val();
	
	
	$('#tt').datagrid({
		url:'/qyChannelSimulatorListen/getQyChannelSimulatorListenListPage',
		queryParams:{
			actionType:actionType,
			online:online,
			version:version,
			osVersion:osVersion,
			heartVersion:heartVersion,
			uuid:uuid,
			location:location,
			clientIp:clientIp,
			clientMac:clientMac
		}
	});
	$('#tt').datagrid('unselectAll');
}

function clearSimulator(){
	var recordArr =$('#tt').datagrid('getChecked');
	if(recordArr==undefined||recordArr==null||recordArr.length==0){
		$.messager.alert("提示", "请先选择记录");
		return;
	}
	
	var uuidArr=[];
	for(var i=0;i<recordArr.length;i++){
		var online=recordArr[i].online;
		if(online=='0'){
			$.messager.alert("提示", "所选记录有在线记录，请重新选择");
			return;
		}
		
		uuidArr[i]=recordArr[i].uuid + recordArr[i].clientMac;
	}
	
	$.messager.confirm('提示', '你确定要清理离线模拟器吗', function(opt){
		if (opt){
			$.ajax({
				url: '/qyChannelSimulatorListen/clearOffline',
				type: 'POST',
				data : {
		            uuids:uuidArr.join(","),
				},
				success : function(data) {
					if (data.return_code == '1') {
						$.messager.alert(market.content.title, "清理成功");
						$("#tt").datagrid('reload');
						$('#tt').datagrid('unselectAll');
					} else {
						$.messager.alert(market.content.title, data.return_msg);
					}
				},
				error : function(e) {
					$.messager.alert("未知错误");
				}
			})
		}
	});
	
}

function clearSimulatorOld(){
	var recordArr =$('#tt').datagrid('getChecked');
	if(recordArr==undefined||recordArr==null||recordArr.length==0){
		$.messager.alert("提示", "请先选择记录");
		return;
	}
	
	var uuidArr=[];
	for(var i=0;i<recordArr.length;i++){
		var online=recordArr[i].online;
		if(online=='0'){
			$.messager.alert("提示", "所选记录有在线记录，请重新选择");
			return;
		}
		
		uuidArr[i]=recordArr[i].uuid;
	}
	
	$.messager.confirm('提示', '你确定要清理离线模拟器吗', function(opt){
		if (opt){
			$.ajax({
				url: '/qyChannelSimulatorListen/clearOffline',
				type: 'POST',
				data : {
		            uuids:uuidArr.join(","),
				},
				success : function(data) {
					if (data.return_code == '1') {
						$.messager.alert(market.content.title, "清理成功");
						$("#tt").datagrid('reload');
						$('#tt').datagrid('unselectAll');
					} else {
						$.messager.alert(market.content.title, data.return_msg);
					}
				},
				error : function(e) {
					$.messager.alert("未知错误");
				}
			})
		}
	});
	
}

function dataOnLoadSuccess(){
	$('#tt').datagrid('unselectAll');
}

function upgradePack(){
	
	var recordArr =$('#tt').datagrid('getChecked');
	if(recordArr==undefined||recordArr==null||recordArr.length==0){
		$.messager.alert("提示", "请先选择记录");
		return;
	}
	
	var version;
	for(var i=0;i<recordArr.length;i++){
		if(version==undefined){
			version=recordArr[i].version;
		}
		if(version!=recordArr[i].version){
			$.messager.alert("提示", "请选择版本号相同的记录");
			return;
		}
		
		if(recordArr[i].actionType==0||recordArr[i].actionType==2){
			$.messager.alert("提示", "所选模拟器已经分配了任务，请重新选择");
			return;
		}
	}
	
	if(version==undefined){
		$.messager.alert("提示", "获取不到版本号");
		return 
	}
	
	$('#upgradeWin').window('open');
	$('#upgradett').datagrid({
		url:'/daemonapk/configList?json&isDelete=0&version=' + version
	});
	$('#upgradeForm').form('clear');
	$("#fromVersionId").val(version);
}

function upgradeSystem(){
	
	var recordArr =$('#tt').datagrid('getChecked');
	if(recordArr==undefined||recordArr==null||recordArr.length==0){
		$.messager.alert("提示", "请先选择记录");
		return;
	}
	
	var osVersion;
	for(var i=0;i<recordArr.length;i++){
		if(osVersion==undefined){
			osVersion=recordArr[i].osVersion;
		}
		if(osVersion!=recordArr[i].osVersion){
			$.messager.alert("提示", "请选择版本号相同的记录");
			return;
		}
		
		if(recordArr[i].actionType==0||recordArr[i].actionType==2){
			$.messager.alert("提示", "所选模拟器已经分配了任务，请重新选择");
			return;
		}
	}
	
	/*if(version==undefined){
		$.messager.alert("提示", "获取不到版本号");
		return 
	}*/
	
	$('#upgradeSystemWin').window('open');
	$('#upgradeSystemDg').datagrid({
		url:'/system/listJson?isDelete=0&version=' + osVersion
	});
	$('#upgradeSystem').form('clear');
	$("#systemfromVersionId").val(osVersion);
}

function upgradeCancel(){
	$('#upgradeWin').window('close');
}

function upgradesubmit(){
	var from=$("#fromVersionId").val();
	var to=$("#toVersionId").val();
	if(from==''||from==undefined){
		$.messager.alert("提示", "版本号不能为空");
		return 
	}
	if(to==''||to==undefined){
		$.messager.alert("提示", "版本号不能为空");
		return 
	}
	if(from==to){
		$.messager.alert("提示", "版本号不能相同");
		return 
	}
	
	var url=$("#filePathId").val();
	var md5=$("#markId").val();
	
	var taskJson={
	    fromVersion:from,
	    toVersion:to,
	    url:url,
	    md5:md5
	}
	
	publishTask(3,JSON.stringify(taskJson));
	
}

function upgradeSystemSubmit(){
	var from=$("#systemfromVersionId").val();
	var to=$("#systemToVersionId").val();
	/*if(from==''||from==undefined){
		$.messager.alert("提示", "版本号不能为空");
		return 
	}*/
	if(to==''||to==undefined){
		$.messager.alert("提示", "版本号不能为空");
		return 
	}
	if(from==to){
		$.messager.alert("提示", "版本号不能相同");
		return 
	}
	
	var url=$("#systemFilePathId").val();
	var md5=$("#systemMarkId").val();
	
	var taskJson={
	    fromVersion:from,
	    toVersion:to,
	    url:url,
	    md5:md5
	}
	
	publishTask(8,JSON.stringify(taskJson));
	
}

function upgradeSystemCancel(){
	$('#upgradeSystemWin').window('close');
}

function closePhone(){
	
	var hasPass=actionTypeValidate();
	if(!hasPass){
		return;
	}
	
	publishTask(2);
}


function restartPhone(){
	var hasPass=actionTypeValidate();
	if(!hasPass){
		return;
	}
	publishTask(1);
}


function stopMark(){
	var hasPass=actionTypeValidate();
	if(!hasPass){
		return;
	}
	
	var recordArr =$('#tt').datagrid('getChecked');
	for(var i=0;i<recordArr.length;i++){
		if(recordArr[i].status==1){
			$.messager.alert("提示", "已选记录中已有停止做mark任务的模拟器，请重新选择");
			return false;
		}
	}
	
	publishTask(4);
}

function restartMark(){
	var hasPass=actionTypeValidate();
	if(!hasPass){
		return;
	}
	var recordArr =$('#tt').datagrid('getChecked');
	for(var i=0;i<recordArr.length;i++){
		if(recordArr[i].status==0){
			$.messager.alert("提示", "已选记录中已有正常做mark任务的模拟器，请重新选择");
			return false;
		}
	}
	publishTask(5);
}

function actionTypeValidate(){
	var recordArr =$('#tt').datagrid('getChecked');
	if(recordArr==null||recordArr.length==0){
		$.messager.alert("提示", "请先选择记录");
		return false;
	}
	
	for(var i=0;i<recordArr.length;i++){
		if(recordArr[i].actionType==0||recordArr[i].actionType==2){
			$.messager.alert("提示", "所选模拟器已经分配了任务，请重新选择");
			return false;
		}
	}
	return true
}


function publishTask(orderType,taskContent){
	var recordArr =$('#tt').datagrid('getChecked');
	if(recordArr==null||recordArr.length==0){
		$.messager.alert("提示", "请先选择记录");
		return;
	}
	
	var uuidArr=new Array();
	for(var i=0;i<recordArr.length;i++){
		uuidArr[i]=recordArr[i].uuid + recordArr[i].clientMac;
	}
	
	$.messager.confirm('提示', '你确定要发布任务吗', function(opt){
		if (opt){
			$.ajax({
				url: '/qyChannelSimulatorListen/publishTask',
				type: 'POST',
				data : {
		            uuids:uuidArr.join(","),
		            orderType:orderType,
		            taskContent:taskContent
				},
				success : function(data) {
					if (data.return_code == '1') {
						$.messager.alert(market.content.title, "发布任务成功");
						$("#tt").datagrid('reload');
						$('#tt').datagrid('unselectAll');
						
						if(orderType=='6') {
							$('#upgradeHeartWin').window('close');
						}
						if(orderType=='3') {
							$('#upgradeWin').window('close');
						}
						if(orderType=='8') {
							$('#upgradeSystemWin').window('close');
						}
						
					} else {
						$.messager.alert(market.content.title, data.return_msg);
					}
				},
				error : function(e) {
					$.messager.alert("未知错误");
				}
			})
		}
	});
	
}

function searchSimulator(){
	var hasPass=actionTypeValidate();
	if(!hasPass){
		return;
	}
	
	publishTask(7)
}

function unSearchSimulator(){
	var hasPass=actionTypeValidate();
	if(!hasPass){
		return;
	}
	publishTask(8)
}

function viewTask(){
	var recordArr =$('#tt').datagrid('getChecked');
	if(recordArr==null||recordArr.length==0){
		$.messager.alert("提示", "请先选择记录");
		return;
	}
	
	var taskId;
	for(var i=0;i<recordArr.length;i++){
		if(taskId==undefined){
			taskId=recordArr[i].taskId;
		}
		if(taskId==undefined||taskId==null||taskId==''||taskId=="null"){
			$.messager.alert("提示", "已选记录中有未做任务记录，请重新选择");
			return;
		}
		
		if(taskId!=recordArr[i].taskId){
			$.messager.alert("提示", "已选记录中有任务不一致，请重新选择");
			return;
		}
	}
	
	if(taskId==undefined){
		$.messager.alert("提示", "已选记录没有执行任务");
		return;
	}
	
	$.ajax({
		url: '/qyChannelSimulatorListen/viewTask',
		type: 'POST',
		data : {
			taskId:taskId
		},
		success : function(data) {
			if (data.return_code == '1') {
				if(data.data==undefined ||data.data==null||data==''||data=="null"){
					$.messager.alert("提示", "获取不到任务");
					return;
				}
				$('#viewTaskWin').window('open');
				$('#taskTypeId').combobox("select",data.data.orderType);
				$('#taskContentId').val(data.data.taskContent);
			} else {
				$.messager.alert(market.content.title, data.return_msg);
			}
		},
		error : function(e) {
			$.messager.alert("未知错误");
		}
	})
}

function clearLocation(){
	$.ajax({
		url: '/qyChannelSimulatorListen/clearLocation',
		type: 'POST',
		success : function(data) {
			if (data.return_code == '1') {
				$.messager.alert(market.content.title, "操作成功");
				$("#tt").datagrid('reload');
				$('#tt').datagrid('unselectAll');
			} else {
				$.messager.alert(market.content.title, data.return_msg);
			}
		},
		error : function(e) {
			$.messager.alert("未知错误");
		}
	})
	
}

function versionDbClick(owIndex, rowData){
	$("#toVersionId").val(rowData.version);
	$("#filePathId").val(rowData.filePath);
	$("#markId").val(rowData.mark);
}

function batchSystemDbClick(owIndex, rowData){
	$("#batchSystemToVersionId").val(rowData.version);
	$("#batchSystemFilePathId").val(rowData.filePath);
	$("#batchSystemMarkId").val(rowData.mark);
}

function systemDbClick(owIndex, rowData){
	$("#systemToVersionId").val(rowData.version);
	$("#systemFilePathId").val(rowData.filePath);
	$("#systemMarkId").val(rowData.mark);
}

function versionOnLoadSuccess(){
	$('#upgradett').datagrid('unselectAll');
}


function cancel(){
	$('#addWin').window('close');
}

function onLineFormatter(value){
	if(value==0){
		return '<span style="color:green">在线</span>';
	}else{
		return '<span style="color:red">离线</span>';
	}
}

function statusFormatter(value){
	if(value==1){
		return '<span style="color:red">暂停做mark任务</span>';
	}else{
		return '<span style="color:green">正常做mark任务</span>';
	}
}

function actionTypeFormatter(value){
	if(value==1){
		return '<span style="color:green">空闲</span>';
	}else if(value==2){
		return '<span style="color:red">正在执行任务</span>';
	}else if(value==3){
		return '<span style="color:green">执行任务完成</span>';
	}else if(value==4){
		return '<span style="color:red">任务执行失败</span>';
	}else if(value==5){
		return '<span style="color:red">拒绝执行任务</span>';
	}else if(value==0){
		return '<span style="color:red">任务待执行</span>';
	}else{
		return '<span style="color:red">未知</span>';
	}
	
}


//升级心跳包
function upgradeHeart(){
	
	var recordArr =$('#tt').datagrid('getChecked');
	if(recordArr==undefined||recordArr==null||recordArr.length==0){
		$.messager.alert("提示", "请先选择记录");
		return;
	}
	
	var heartVersion;
	for(var i=0;i<recordArr.length;i++){
		if(heartVersion==undefined){
			heartVersion=recordArr[i].heartVersion;
		}
		if(heartVersion!=recordArr[i].heartVersion){
			$.messager.alert("提示", "请选择版本号相同的记录");
			return;
		}
		
		if(recordArr[i].actionType==0||recordArr[i].actionType==2){
			$.messager.alert("提示", "所选模拟器已经分配了任务，请重新选择");
			return;
		}
	}
	
	if(heartVersion==undefined){
		$.messager.alert("提示", "获取不到版本号");
		return 
	}
	
	$('#upgradeHeartWin').window('open');
	$('#upgradehearttt').datagrid({
		url:'/heartbeat/listJson?isDelete=0&version=' + heartVersion
	});
	$('#upgradeHeartForm').form('clear');
	$("#heartfromVersionId").val(heartVersion);
}

function upgradeHeartCancel(){
	$('#upgradeHeartWin').window('close');
}

function upgradeHeartsubmit(){
	var from=$("#heartfromVersionId").val();
	var to=$("#hearttoVersionId").val();
	if(from==''||from==undefined){
		$.messager.alert("提示", "版本号不能为空");
		return 
	}
	if(to==''||to==undefined){
		$.messager.alert("提示", "版本号不能为空");
		return 
	}
	if(from==to){
		$.messager.alert("提示", "版本号不能相同");
		return 
	}
	
	var url=$("#heartfilePathId").val();
	var md5=$("#heartmd5Id").val();
	//var bucketName=$("#heartbucketNameId").val();
	
	var taskJson={
	    fromVersion:from,
	    toVersion:to,
	    url:url,
	    md5:md5
	}
	
	publishTask(6,JSON.stringify(taskJson));
	
}

function heartDbClick(owIndex, rowData){
	console.info(rowData);
	$("#hearttoVersionId").val(rowData.version);
	$("#heartfilePathId").val(rowData.filePath);
	$("#heartmd5Id").val(rowData.mark);
	//$("#heartbucketNameId").val(rowData.bucketName);
}

function heartOnLoadSuccess(){
	$('#upgradehearttt').datagrid('unselectAll');
}

function setNum(){
	$('#setNumWin').window('open');
	$('#setNumForm').form('clear');
}

function setNumCancel(){
	$('#setNumWin').window('close');
	$('#setNumForm').form('clear');
}

function setNumSubmit(){
	var upgradeSystemNum = $('#upgradeSystemNum').val();
	var upgradeSystemIntervalTime = $('#upgradeSystemIntervalTime').val();
	
	$.ajax({
	    url:'/qyChannelSimulatorListen/setNum',
	    type:'POST',
	    data:{
	    	upgradeSystemNum : upgradeSystemNum,
	    	upgradeSystemIntervalTime : upgradeSystemIntervalTime
	    },
	    async:true,
	    dataType: 'json',
	    success:function(data,textStatus,jqXHR){
	    	if (data.return_code == '1') {
				$.messager.alert("提示", data.return_msg);
				$('#setNumWin').window('close');
		    	$('#setNumForm').form('clear');
			} else {
				$.messager.alert("提示", data.return_msg);
			}
	    },
	    error:function(xhr,textStatus){
	    	$.messager.alert("未知错误");
	    }
	})
}

function batchUpgradeSystem(){
	var recordArr =$('#tt').datagrid('getChecked');
	if(recordArr==undefined||recordArr==null||recordArr.length==0){
		$.messager.alert("提示", "请先选择记录");
		return;
	}
	
	var osVersion;
	for(var i=0;i<recordArr.length;i++){
		if(osVersion==undefined){
			osVersion=recordArr[i].osVersion;
		}
		if(osVersion!=recordArr[i].osVersion){
			$.messager.alert("提示", "请选择版本号相同的记录");
			return;
		}
		
		if(recordArr[i].actionType==0||recordArr[i].actionType==2){
			$.messager.alert("提示", "所选模拟器已经分配了任务，请重新选择");
			return;
		}
	}
	
	$('#batchUpgradeSystemWin').window('open');
	$('#batchUpgradeSystemDg').datagrid({
		url:'/system/listJson?isDelete=0&version=' + osVersion
	});
	
	$('#batchUpgradeSystem').form('clear');
	$("#batchSystemFromVersionId").val(osVersion);
}

function batchUpgradeSystemSubmit(){
	var from=$("#batchSystemFromVersionId").val();
	var to=$("#batchSystemToVersionId").val();
	/*if(from==''||from==undefined){
		$.messager.alert("提示", "版本号不能为空");
		return 
	}*/
	if(to==''||to==undefined){
		$.messager.alert("提示", "版本号不能为空");
		return 
	}
	if(from==to){
		$.messager.alert("提示", "版本号不能相同");
		return 
	}
	
	var url=$("#batchSystemFilePathId").val();
	var md5=$("#batchSystemMarkId").val();
	
	var taskJson={
	    fromVersion:from,
	    toVersion:to,
	    url:url,
	    md5:md5
	}
	
	batchPublishTask(8,JSON.stringify(taskJson));
}

function batchPublishTask(orderType,taskContent){
	var recordArr =$('#tt').datagrid('getChecked');
	if(recordArr==null||recordArr.length==0){
		$.messager.alert("提示", "请先选择记录");
		return;
	}
	
	var uuidArr=new Array();
	for(var i=0;i<recordArr.length;i++){
		uuidArr[i]=recordArr[i].uuid + recordArr[i].clientMac;
	}
	
	$.messager.confirm('提示', '你确定要发布任务吗', function(opt){
		if (opt){
			$.ajax({
				url: '/qyChannelSimulatorListen/batchPublishTask',
				type: 'POST',
				data : {
		            uuids:uuidArr.join(","),
		            orderType:orderType,
		            taskContent:taskContent
				},
				success : function(data) {
					if (data.return_code == '1') {
						$.messager.alert(market.content.title, "发布任务成功");
						$("#tt").datagrid('reload');
						$('#tt').datagrid('unselectAll');
						$('#batchUpgradeSystemWin').window('close');
					} else {
						$.messager.alert(market.content.title, data.return_msg);
					}
				},
				error : function(e) {
					$.messager.alert("未知错误");
				}
			})
		}
	});
}

function batchUpgradeSystemCancel(){
	$('#batchUpgradeSystemWin').window('close');
	$('#batchUpgradeSystem').form('clear');
}

